
{% extends 'layout/index.html' %}

{% load custom_tag %}
{% block css %}

{% endblock %}

{% block conent %}

    <ol class="breadcrumb">
      <li> <a href="/">主机管理</a></li>
      <li  class="active">主机展示</li>
    </ol>

    <div class="panel-body" >
        <div class="col-lg-3" >
             <div class="panel" >
                <div class="btn btn-primary btn-block margin-bottom" >
                    <h2 class="panel-title" style="font-size: 20px">主机组</h2>
                </div>

                <div class="pannel-body">
                    <div class="list-group bord-no">

                        {% ifequal selected_g_id '-1' %}
                            <a class="list-group-item active" href="/host/hosts/?selected_group=-1"><b>最近登录 [{{ recent_logins|length }}]</b></a>
                        {% else %}
                             <a class="list-group-item" style="background-color: #ecf0f5"  href="/host/hosts/?selected_group=-1">最近登录 [{{ recent_logins|length }}]</a>
                        {% endifequal %}

                        {% for g in request.user.host_groups.select_related %}

                            {% ifequal g.id selected_g_id %}
                                <a class="list-group-item active"   href="{% url 'host_list'  %}?selected_group={{g.id}}"><b>{{g.name }} [{{ g.bind_hosts.select_related.count }}]</b></a>
                            {% else %}
                                <a class="list-group-item" style="background-color: #ecf0f5"  href="{% url 'host_list'  %}?selected_group={{g.id}}"><b>{{g.name }} [{{  g.bind_hosts.select_related.count}}]</b></a>
                            {% endifequal %}

                        {% endfor %}

                        {% ifequal selected_g_id '-2' %}
                            <a class="list-group-item active" href="/host/hosts/?selected_group=-2"><b>全部主机 [{{ request.user.bind_hosts.select_related.count }}]</b></a>
                        {% else %}
                             <a class="list-group-item"  style="background-color: #ecf0f5" href="/host/hosts/?selected_group=-2">全部主机 [{{ request.user.bind_hosts.select_related.count }}]</a>
                        {% endifequal %}


                    </div>

                </div>

             </div>

        </div>

        <div class="col-lg-9" >
              <div class="box">
                <div class="box-header" >
                  <h3 class="box-title" style="font-size: 19px" > 主机列表</h3>

                  <div class="box-tools">
                    <div class="input-group input-group-sm" style="width: 220px;">
                      <input type="text" name="table_search" class="form-control pull-right" placeholder="Search">

                      <div class="input-group-btn">
                        <button type="submit" class="btn btn-default"><i class="fa fa-search"></i></button>
                      </div>
                    </div>
                  </div>
                </div>
                <!-- /.box-header -->
                <div class="box-body table-responsive no-padding">
                    <table class="table table-hover">
                        <thead>
                            <tr>
                                <th class="text-center">ID</th>
                                <th>主机名称</th>
                                <th>IP地址</th>
                                <th>系统</th>
                                <th>状态</th>
                                <th>绑定用户</th>
                                <th>临时Token</th>
                                <th>登录</th>
                            </tr>
                        </thead>

                        <tbody>

                           <!--已经分组的机器 -->
                           {% for g in request.user.host_groups.select_related  %}
                                {% ifequal selected_g_id g.id %}
                                    {% for host in g.bind_hosts.select_related %}
                                        <tr>
                                            <td class="text-center">{{ forloop.counter }}</td>
                                            <td><a href="#" class="btn-link">{{ host.host.hostname }}</a></td>
                                            <td><a href="#" class="">{{ host.host.manage_ip }}</a></td>
                                            <td> {{ host.host.os_version }} </td>
                                            <td><a href="#" class="">{% device_status_display host %}</a></td>
                                            <td><span class="">{{ host.remote_user }}</span> </td>
                                            <td><span class=""></span></td>
                                            <td>
                                                <a bind_host_id="{{ host.host.id }}" class="label label-info cursor gen_token">生成Token</a>
                                                <a href="http://{{ webssh.host }}:{{ webssh.port }}?u={{ webssh.username }}&p={{ webssh.password }}" target="_blank"><span class="label label-success cursor host_login">Login</span></a>
                                            </td>

                                        </tr>
                                    {% endfor %}
                                {% endifequal %}
                           {% endfor %}

                           <!-- 未分组主机-quanbu-->
                           {% if selected_g_id == '-2' %}
                                    {% for host in request.user.bind_hosts.select_related %}
                                        <tr>
                                            <td class="text-center">{{ forloop.counter }}</td>
                                            <td><a href="#" class="btn-link">{{ host.host.hostname }}</a></td>
                                            <td><a href="#" class="">{{ host.host.manage_ip }}</a></td>
                                            <td>{{ host.host.os_version }}</td>
                                            <td><a href="#" class="">{% device_status_display host %}</a></td>
                                            <td><span class="">{{ host.remote_user }}</span> </td>
                                            <td><span class=""></span></td>
                                            <td>
                                                <a bind_host_id="{{ host.host.id }}" class="label label-info cursor gen_token">生成Token</a>
                                                <a href="http://{{ webssh.host }}:{{ webssh.port }}?u={{ webssh.username }}&p={{ webssh.password }}" target="_blank"><span class="label label-success cursor host_login">Login</span></a>
                                            </td>
                                        </tr>
                                    {% endfor %}
                            {% endif %}

                           <!-- 最近登录主机-->
                           {% if selected_g_id == '-1' %}
                                    {% for host in  recent_logins %}
                                        <tr>
                                            <td class="text-center">{{ forloop.counter }}</td>
                                            <td><a href="#" class="btn-link">{{ host.host.host.hostname }}</a></td>
                                            <td><a href="#" class="">{{ host.host.host.manage_ip }}</a></td>
                                            <td>{{ host.host.host.os_version }}</td>
                                            <td><a href="#" class=""> {% device_status_display host.host %}</a></td>
                                            <td><span class="">{{ host.host.remote_user }}</span></td>
                                            <td><span class=""></span></td>
                                            <td>
                                                <a bind_host_id="{{ host.host.id }}" class="label label-info cursor gen_token">生成Token</a>
                                                <a href="http://{{ webssh.host }}:{{ webssh.port }}?u={{ webssh.username }}&p={{ webssh.password }}" target="_blank"><span class="label label-success cursor host_login">Login</span></a>
                                            </td>
                                        </tr>
                                    {% endfor %}
                           {% endif %}

                        </tbody>

                    </table>
                </div>
                <!-- /.box-body -->
              </div>
              <!-- /.box -->
        </div>

    </div>


{% endblock %}

{% block js %}
    <script type="text/javascript" >
        $(document).ready(function(){

           $(".gen_token").click(function(){
               var bind_host_id = $(this).attr("bind_host_id");
               var token_ele = $(this).parent().prev().children("span");
               $.post("{% url 'token_gen' %}", {'token_type':'host_token','bind_host_id':bind_host_id},function(callback){
                   var token_obj = JSON.parse(callback);
                   //console.log('--token' + token_obj.token);
                   token_ele.html(token_obj.token);
               });//end post

           }) ;
        });

    </script>

{% endblock %}